package cn.sxt.usercenter.users.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.usercenter.api.pojo.AAdmins;
import cn.sxt.usercenter.api.pojo.AUsers;
import cn.sxt.usercenter.api.pojo.AUsersExtend;
import cn.sxt.usercenter.api.service.IUserService;
import cn.sxt.usercenter.common.util.PageInfoUtil;
import cn.sxt.usercenter.users.dao.IAAdminsDao;
import cn.sxt.usercenter.users.dao.IAUsersDao;
import cn.sxt.usercenter.users.dao.IAUsersExtendDao;


@Service("userService")
public class UserServiceImpl implements IUserService
{

	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IAUsersExtendDao userExtendDao;
	@Resource 
	private IAUsersDao usersDao;
	
	//查询一个管理员
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}

	//查询多个管理员
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if(pageInfoUtil != null)
		{
			//查询分页
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<AAdmins> adminsList=this.adminsDao.findCondList(condMap);
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminsList;
		}
		return this.adminsDao.findCondList(condMap);
	}

	//添加一个管理员
	@Override
	public JSONObject saveOneAdmins(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.save(admins);
		if(res>0)
		{
			//存储数据
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	//更新一个管理员
	@Override
	public JSONObject updateOneAdmins(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.update(admins);
		if(res>0)
		{
			//存储数据
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	
	/**
	 * 
	 * 用户模块
	 * */
	
	//查询一个用户
		@Override
		public AUsers findOneUsersService(Map<String, Object> condMap)
		{
			return this.usersDao.findOne(condMap);
		}

		//查询多个用户
		@Override
		public List<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
		{
			if(pageInfoUtil != null)
			{
				//查询分页
				Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
				List<AUsers> usersList=this.usersDao.findCondList(condMap);
				//设置总记录数
				pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
				return usersList;
			}
			return this.usersDao.findCondList(condMap);
		}

		//添加一个用户
		@Override
		public JSONObject saveOneUsers(AUsers users)
		{
			JSONObject resultJSON=new JSONObject();
			int res=this.usersDao.save(users);
			if(res>0)
			{
				//存储数据
				JSONObject dataJSON=new JSONObject();
				dataJSON.put("id", users.getId());
				dataJSON.put("effect", res);
				
				resultJSON.put("data", dataJSON);
				resultJSON.put("code", "0");
				resultJSON.put("info", "成功");
				
				//保存用户的扩展信息
				AUsersExtend usersExtend=new AUsersExtend();
				usersExtend.setUsersId(users.getId());
				usersExtend.setCreateTime(new Date());
				usersExtend.setUpdateTime(new Date());
				users.setUsersExtend(usersExtend);
				
				this.saveOneAUsersExtend(usersExtend);
				
			}else
			{
				
				resultJSON.put("code", "1");
				resultJSON.put("info", "失败");
			}
			return resultJSON;
		}

		//更新一个用户
		@Override
		public JSONObject updateOneUsers(AUsers users)
		{
			JSONObject resultJSON=new JSONObject();
			int res=this.usersDao.update(users);
			if(res>0)
			{
				//存储数据
				JSONObject dataJSON=new JSONObject();
				dataJSON.put("id", users.getId());
				dataJSON.put("effect", res);
				
				resultJSON.put("data", dataJSON);
				resultJSON.put("code", "0");
				resultJSON.put("info", "成功");
			}else
			{
				
				resultJSON.put("code", "1");
				resultJSON.put("info", "失败");
			}
			return resultJSON;
		}
		
	
		/**
		 * 
		 * 用户扩展模块
		 * */
		
		//查询一个用户扩展
			@Override
			public AUsersExtend findOneAUsersExtendService(Map<String, Object> condMap)
			{
				return this.userExtendDao.findOne(condMap);
			}

			//查询多个用户扩展
			@Override
			public List<AUsersExtend> findCondListAUsersExtendService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
			{
				if(pageInfoUtil != null)
				{
					//查询分页
					Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
					List<AUsersExtend> usersExtendList=this.userExtendDao.findCondList(condMap);
					//设置总记录数
					pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
					return usersExtendList;
				}
				return this.userExtendDao.findCondList(condMap);
			}

			//添加一个用户扩展
			@Override
			public JSONObject saveOneAUsersExtend(AUsersExtend usersExtend)
			{
				JSONObject resultJSON=new JSONObject();
				int res=this.userExtendDao.save(usersExtend);
				if(res>0)
				{
					//存储数据
					JSONObject dataJSON=new JSONObject();
					dataJSON.put("id", usersExtend.getId());
					dataJSON.put("effect", res);
					
					resultJSON.put("data", dataJSON);
					resultJSON.put("code", "0");
					resultJSON.put("info", "成功");
				}else
				{
					
					resultJSON.put("code", "1");
					resultJSON.put("info", "失败");
				}
				return resultJSON;
			}

			//更新一个用户扩展
			@Override
			public JSONObject updateOneAUsersExtend(AUsersExtend usersExtend)
			{
				JSONObject resultJSON=new JSONObject();
				int res=this.userExtendDao.update(usersExtend);
				if(res>0)
				{
					//存储数据
					JSONObject dataJSON=new JSONObject();
					dataJSON.put("id", usersExtend.getId());
					dataJSON.put("effect", res);
					
					resultJSON.put("data", dataJSON);
					resultJSON.put("code", "0");
					resultJSON.put("info", "成功");
				}else
				{
					
					resultJSON.put("code", "1");
					resultJSON.put("info", "失败");
				}
				return resultJSON;
			}
			
		
}
